/**
 * This code is originally written by Lars Vogel
 * http://www.vogella.de/articles/AndroidListView/article.html
 * and modified by me.
 *
 * @author prebz
 */

package com.wirelesstransfer.activity.status;

import java.util.List;

import com.wirelesstransfer.R;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ProgressBar;
import android.widget.TextView;

public class CustomStatusArrayAdapter extends ArrayAdapter<String> {

	private final Activity context;
	private final List<String> allIps;
	
	/**
	 * 
	 * @param context from activity which will create the adapter
	 * @param names of all the files
	 * @param allFilesSize the size of all the files
	 */
	public CustomStatusArrayAdapter(Activity context, List<String> allIps) {
		super(context, R.layout.rowstatus, allIps);
		this.context = context;
		this.allIps = allIps;
	}
	
	// saves the reference of the object in list
	static class ViewHolder {
		public TextView textViewIp;
		public ProgressBar progressBarFiles;
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		ViewHolder holder;

		// reuse existing views for performance
		View rowView = convertView;
		if (rowView == null) {
			LayoutInflater inflater = context.getLayoutInflater();
			rowView = inflater.inflate(R.layout.rowstatus, null, true);
			holder = new ViewHolder();
			holder.textViewIp = (TextView) rowView.findViewById(R.id.status_row_connection_txt);
			holder.progressBarFiles = (ProgressBar) rowView.findViewById(R.id.status_progressbar);
			rowView.setTag(holder);
		} else {
			holder = (ViewHolder) rowView.getTag();
		}

		holder.textViewIp.setText(allIps.get(position));
		
		//for demo purposes 
		int rand = (int) (Math.random() * 90) + 10;
		holder.progressBarFiles.setProgress(rand);
				
		return rowView;
	}
	
}
